<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>The Request Object - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.controller.request.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.controller.request.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.controller.front.html">The Front Controller</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.controller.html">Zend_Controller</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.controller.router.html">The Standard Router</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.controller.request" class="section"><div class="info"><h1 class="title">The Request Object</h1></div>
    

    <div class="section" id="zend.controller.request.introduction"><div class="info"><h1 class="title">Introduction</h1></div>
        

        <p class="para">
            The request object is a simple value object that is passed between
            <span class="classname">Zend_Controller_Front</span> and the router, dispatcher, and
            controller classes. It packages the names of the requested module,
            controller, action, and optional parameters, as well as the rest of
            the request environment, be it <acronym class="acronym">HTTP</acronym>, the <acronym class="acronym">CLI</acronym>, or
            <acronym class="acronym">PHP</acronym>-GTK.
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    The module name is accessed by  <span class="methodname">getModuleName()</span> and
                     <span class="methodname">setModuleName()</span>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    The controller name is accessed by  <span class="methodname">getControllerName()</span>
                    and  <span class="methodname">setControllerName()</span>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    The name of the action to call within that controller is
                    accessed by  <span class="methodname">getActionName()</span> and
                     <span class="methodname">setActionName()</span>.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    Parameters to be accessible by the action are an associative array
                    of key and value pairs that are retrieved by
                     <span class="methodname">getParams()</span> and set with
                     <span class="methodname">setParams()</span>, or individually by
                     <span class="methodname">getParam()</span> and  <span class="methodname">setParam()</span>.
                </p>
            </li>
        </ul>

        <p class="para">
            Based on the type of request, there may be more methods available.
            The default request used, <span class="classname">Zend_Controller_Request_Http</span>,
            for instance, has methods for retrieving the request <acronym class="acronym">URI</acronym>, path
            information, <var class="varname">$_GET</var> and <var class="varname">$_POST</var> parameters,
            etc.
        </p>

        <p class="para">
            The request object is passed to the front controller, or if none is
            provided, it is instantiated at the beginning of the dispatch
            process, before routing occurs. It is passed through to every object
            in the dispatch chain.
        </p>

        <p class="para">
            Additionally, the request object is particularly useful in testing.
            The developer may craft the request environment, including module,
            controller, action, parameters, <acronym class="acronym">URI</acronym>, etc, and pass the request
            object to the front controller to test application flow. When paired
            with the <a href="zend.controller.response.html" class="link">response
                object</a>, elaborate and precise unit testing of <acronym class="acronym">MVC</acronym>
            applications becomes possible.
        </p>
    </div>

    <div class="section" id="zend.controller.request.http"><div class="info"><h1 class="title">HTTP Requests</h1></div>
        

        <div class="section" id="zend.controller.request.http.dataacess"><div class="info"><h1 class="title">Accessing Request Data</h1></div>
            

            <p class="para">
                <span class="classname">Zend_Controller_Request_Http</span> encapsulates access to
                relevant values such as the key name and value for the
                controller and action router variables, and all additional
                parameters parsed from the <acronym class="acronym">URI</acronym>. It additionally allows access to
                values contained in the superglobals as public members, and
                manages the current Base <acronym class="acronym">URL</acronym> and Request <acronym class="acronym">URI</acronym>.
                Superglobal values cannot be set on a request object, instead use the
                 <span class="methodname">setParam()</span> and  <span class="methodname">getParam()</span> methods
                to set or retrieve user parameters.
            </p>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Superglobal Data</b><br /></span>
                

                <p class="para">
                    When accessing superglobal data through
                    <span class="classname">Zend_Controller_Request_Http</span> as public member
                    properties, it is necessary to keep in mind that the
                    property name (superglobal array key) is matched to a
                    superglobal in a specific order of precedence: 1. <b><tt>GET</tt></b>, 2.
                    <b><tt>POST</tt></b>, 3. <b><tt>COOKIE</tt></b>, 4.
                    <b><tt>SERVER</tt></b>, 5. <b><tt>ENV</tt></b>.
                </p>
            </p></blockquote>

            <p class="para">
                Specific superglobals can be accessed using a public method as
                an alternative. For example, the raw value of
                <var class="varname">$_POST[&#039;user&#039;]</var> can be accessed by calling
                 <span class="methodname">getPost(&#039;user&#039;)</span> on the request object. These
                include  <span class="methodname">getQuery()</span> for retrieving
                <var class="varname">$_GET</var> elements, and  <span class="methodname">getHeader()</span> for
                retrieving request headers.
            </p>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>GET and POST Data</b><br /></span>
                

                <p class="para">
                    Be cautious when accessing data from the request object as
                    it is not filtered in any way. The router and dispatcher
                    validate and filter data for use with their tasks, but leave
                    the data untouched in the request object.
                </p>
            </p></blockquote>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Retrieving the Raw POST Data</b><br /></span>
                

                <p class="para">
                    As of 1.5.0, you can also retrieve the raw post data via the
                     <span class="methodname">getRawBody()</span> method. This method returns
                    <b><tt>FALSE</tt></b> if no data was submitted in that fashion, but the
                    full body of the post otherwise.
                </p>

                <p class="para">
                    This is primarily useful for accepting content when
                    developing a RESTful <acronym class="acronym">MVC</acronym> application.
                </p>
            </p></blockquote>

            <p class="para">
                You may also set user parameters in the request object using
                 <span class="methodname">setParam()</span> and retrieve these later using
                 <span class="methodname">getParam()</span>. The router makes use of this
                functionality to set parameters matched in the request <acronym class="acronym">URI</acronym> into
                the request object.
            </p>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>getParam() Retrieves More than User Parameters</b><br /></span>
                

                <p class="para">
                    In order to do some of its work,  <span class="methodname">getParam()</span> actually
                    retrieves from several sources. In order of priority, these
                    include: user parameters set via  <span class="methodname">setParam()</span>,
                    <b><tt>GET</tt></b> parameters, and finally <b><tt>POST</tt></b>
                    parameters. Be aware of this when pulling data via this
                    method.
                </p>

                <p class="para">
                    If you wish to pull only from parameters you set via
                     <span class="methodname">setParam()</span>, use the
                     <span class="methodname">getUserParam()</span>.
                </p>

                <p class="para">
                    Additionally, as of 1.5.0, you can lock down which parameter
                    sources will be searched.  <span class="methodname">setParamSources()</span>
                    allows you to specify an empty array or an array with one or
                    more of the values &#039;_GET&#039; or &#039;_POST&#039; indicating which
                    parameter sources are allowed (by default, both are
                    allowed); if you wish to restrict access to only &#039;_GET&#039;
                    specify  <span class="methodname">setParamSources(array(&#039;_GET&#039;))</span>.
                </p>
            </p></blockquote>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Apache Quirks</b><br /></span>
                

                <p class="para">
                    If you are using Apache&#039;s 404 handler to pass incoming
                    requests to the front controller, or using a PT flag with
                    rewrite rules, <var class="varname">$_SERVER[&#039;REDIRECT_URL&#039;]</var>
                    contains the <acronym class="acronym">URI</acronym> you need, not
                    <var class="varname">$_SERVER[&#039;REQUEST_URI&#039;]</var>. If you are using such
                    a setup and getting invalid routing, you should use the
                    <span class="classname">Zend_Controller_Request_Apache404</span> class instead
                    of the default <acronym class="acronym">HTTP</acronym> class for your request object:
                </p>

                <pre class="programlisting brush: php">
$request = new Zend_Controller_Request_Apache404();
$front-&gt;setRequest($request);
</pre>


                <p class="para">
                    This class extends the
                    <span class="classname">Zend_Controller_Request_Http</span> class and simply
                    modifies the autodiscovery of the request <acronym class="acronym">URI</acronym>. It can be
                    used as a drop-in replacement.
                </p>
            </p></blockquote>
        </div>

        <div class="section" id="zend.controller.request.http.baseurl"><div class="info"><h1 class="title">Base Url and Subdirectories</h1></div>
            

            <p class="para">
                <span class="classname">Zend_Controller_Request_Http</span> allows
                <span class="classname">Zend_Controller_Router_Rewrite</span> to be used in subdirectories.
                <span class="classname">Zend_Controller_Request_Http</span> will attempt to automatically
                detect your base <acronym class="acronym">URL</acronym> and set it accordingly.
            </p>

            <p class="para">
                For example, if you keep your <var class="filename">index.php</var> in a
                webserver subdirectory named
                <var class="filename">/projects/myapp/index.php</var>, base <acronym class="acronym">URL</acronym> (rewrite
                base) should be set to <var class="filename">/projects/myapp</var>. This string will
                then be stripped from the beginning of the path before
                calculating any route matches. This frees one from the necessity
                of prepending it to any of your routes. A route of
                <strong class="command">&#039;user/:username&#039;</strong> will match <acronym class="acronym">URI</acronym>s like
                <var class="filename">http://localhost/projects/myapp/user/martel</var> and
                <var class="filename">http://example.com/user/martel</var>.
            </p>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>URL Detection is Case Sensitive</b><br /></span>
                

                <p class="para">
                    Automatic base <acronym class="acronym">URL</acronym> detection is case sensitive, so make sure
                    your <acronym class="acronym">URL</acronym> will match a subdirectory name in a filesystem (even
                    on Windows machines). If it doesn&#039;t, an exception will be raised.
                </p>
            </p></blockquote>

            <p class="para">
                Should base <acronym class="acronym">URL</acronym> be detected incorrectly you can override it
                with your own base path with the help of the
                 <span class="methodname">setBaseUrl()</span> method of either the
                <span class="classname">Zend_Controller_Request_Http</span> class, or the
                <span class="classname">Zend_Controller_Front</span> class. The easiest
                method is to set it in <span class="classname">Zend_Controller_Front</span>,
                which will proxy it into the request object. Example usage to
                set a custom base <acronym class="acronym">URL</acronym>:
            </p>

            <pre class="programlisting brush: php">
/**
 * Dispatch Request with custom base URL with Zend_Controller_Front.
 */
$router     = new Zend_Controller_Router_Rewrite();
$controller = Zend_Controller_Front::getInstance();
$controller-&gt;setControllerDirectory(&#039;./application/controllers&#039;)
           -&gt;setRouter($router)
           -&gt;setBaseUrl(&#039;/projects/myapp&#039;); // set the base url!
$response   = $controller-&gt;dispatch();
</pre>

        </div>

        <div class="section" id="zend.controller.request.http.method"><div class="info"><h1 class="title">Determining the Request Method</h1></div>
            

            <p class="para">
                 <span class="methodname">getMethod()</span> allows you to determine the
                <acronym class="acronym">HTTP</acronym> request method used to request the current resource.
                Additionally, a variety of methods exist that allow you to get
                boolean responses when asking if a specific type of request has
                been made:
            </p>

            <ul class="itemizedlist">
                <li class="listitem"><p class="para"> <span class="methodname">isGet()</span></p></li>
                <li class="listitem"><p class="para"> <span class="methodname">isPost()</span></p></li>
                <li class="listitem"><p class="para"> <span class="methodname">isPut()</span></p></li>
                <li class="listitem"><p class="para"> <span class="methodname">isDelete()</span></p></li>
                <li class="listitem"><p class="para"> <span class="methodname">isHead()</span></p></li>
                <li class="listitem"><p class="para"> <span class="methodname">isOptions()</span></p></li>
            </ul>

            <p class="para">
                The primary use case for these is for creating RESTful <acronym class="acronym">MVC</acronym>
                architectures.
            </p>
        </div>

        <div class="section" id="zend.controller.request.http.ajax"><div class="info"><h1 class="title">Detecting AJAX Requests</h1></div>
            

            <p class="para">
                <span class="classname">Zend_Controller_Request_Http</span> has a rudimentary
                method for detecting <acronym class="acronym">AJAX</acronym> requests:
                 <span class="methodname">isXmlHttpRequest()</span>. This method looks for an
                <acronym class="acronym">HTTP</acronym> request header <em class="emphasis">X-Requested-With</em> with
                the value &#039;XMLHttpRequest&#039;; if found, it returns <b><tt>TRUE</tt></b>.
            </p>

            <p class="para">
                Currently, this header is known to be passed by default with the
                following JS libraries:
            </p>

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">Prototype and Scriptaculous (and libraries derived from Prototype)</p>
                </li>

                <li class="listitem"><p class="para">Yahoo! UI Library</p></li>
                <li class="listitem"><p class="para">jQuery</p></li>
                <li class="listitem"><p class="para">MochiKit</p></li>
            </ul>

            <p class="para">
                Most <acronym class="acronym">AJAX</acronym> libraries allow you to send custom
                <acronym class="acronym">HTTP</acronym> request headers; if your library does not send this header,
                simply add it as a request header to ensure the
                 <span class="methodname">isXmlHttpRequest()</span> method works for you.
            </p>
        </div>
    </div>

    <div class="section" id="zend.controller.request.subclassing"><div class="info"><h1 class="title">Subclassing the Request Object</h1></div>
        

        <p class="para">
            The base request class used for all request objects is the abstract
            class <span class="classname">Zend_Controller_Request_Abstract</span>. At its most
            basic, it defines the following methods:
        </p>

        <pre class="programlisting brush: php">
abstract class Zend_Controller_Request_Abstract
{
    /**
     * @return string
     */
    public function getControllerName();

    /**
     * @param string $value
     * @return self
     */
    public function setControllerName($value);

    /**
     * @return string
     */
    public function getActionName();

    /**
     * @param string $value
     * @return self
     */
    public function setActionName($value);

    /**
     * @return string
     */
    public function getControllerKey();

    /**
     * @param string $key
     * @return self
     */
    public function setControllerKey($key);

    /**
     * @return string
     */
    public function getActionKey();

    /**
     * @param string $key
     * @return self
     */
    public function setActionKey($key);

    /**
     * @param string $key
     * @return mixed
     */
    public function getParam($key);

    /**
     * @param string $key
     * @param mixed $value
     * @return self
     */
    public function setParam($key, $value);

    /**
     * @return array
     */
     public function getParams();

    /**
     * @param array $array
     * @return self
     */
    public function setParams(array $array);

    /**
     * @param boolean $flag
     * @return self
     */
    public function setDispatched($flag = true);

    /**
     * @return boolean
     */
    public function isDispatched();
}
</pre>


        <p class="para">
            The request object is a container for the request environment. The
            controller chain really only needs to know how to set and retrieve the
            controller, action, optional parameters, and dispatched status. By
            default, the request will search its own parameters using the
            controller or action keys in order to determine the controller and
            action.
        </p>

        <p class="para">
            Extend this class, or one of its derivatives, when you need the
            request class to interact with a specific environment in order to
            retrieve data for use in the above tasks. Examples include <a href="zend.controller.request.html#zend.controller.request.http" class="link">the <acronym class="acronym">HTTP</acronym>
                environment</a>, a <acronym class="acronym">CLI</acronym> environment, or a
            <acronym class="acronym">PHP</acronym>-GTK environment.
        </p>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.controller.front.html">The Front Controller</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.controller.html">Zend_Controller</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.controller.router.html">The Standard Router</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.controller.html">Zend_Controller</a></li>
  <li><a href="zend.controller.quickstart.html">Guia de In&iacute;cio R&aacute;pido do Zend_Controller</a></li>
  <li><a href="zend.controller.basics.html">O B&aacute;sico de Zend_Controller</a></li>
  <li><a href="zend.controller.front.html">The Front Controller</a></li>
  <li class="active"><a href="zend.controller.request.html">The Request Object</a></li>
  <li><a href="zend.controller.router.html">The Standard Router</a></li>
  <li><a href="zend.controller.dispatcher.html">The Dispatcher</a></li>
  <li><a href="zend.controller.action.html">Action Controllers</a></li>
  <li><a href="zend.controller.actionhelpers.html">Action Helpers</a></li>
  <li><a href="zend.controller.response.html">The Response Object</a></li>
  <li><a href="zend.controller.plugins.html">Plugins</a></li>
  <li><a href="zend.controller.modular.html">Using a Conventional Modular Directory Structure</a></li>
  <li><a href="zend.controller.exceptions.html">MVC Exceptions</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>